<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="户籍性质" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择户籍性质" clearable size="small">
          <el-option
            v-for="dict in dict.type.household_registration_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="户主姓名"  prop="name">
        <el-input v-model="queryParams.name"></el-input>
      </el-form-item>
      <el-form-item label="身份证号"  prop="idCard">
        <el-input v-model="queryParams.idCard"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['residence:residence:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['residence:residence:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="residenceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center" prop="id"/>
      <el-table-column label="户籍性质" align="center" prop="type" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.household_registration_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="户号" align="center" prop="householdsNo"/>
      <el-table-column label="是否为军属" align="center" prop="isMilitaryFamilies" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isMilitaryFamilies"/>
        </template>
      </el-table-column>
      <el-table-column label="户主身份证号" align="center" prop="idCard" show-overflow-tooltip/>
      <el-table-column label="户主姓名" align="center" prop="name"/>
      <el-table-column label="省" align="center" prop="province"/>
      <el-table-column label="市" align="center" prop="city"/>
      <el-table-column label="区县" align="center" prop="county"/>
      <el-table-column label="户籍所在地" align="center" prop="address" show-overflow-tooltip/>
      <el-table-column label="签证机关" align="center" prop="visaAgencies"/>
      <el-table-column label="户籍状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.residence_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="人口数" align="center" prop="peopleCount"/>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-if="scope.row.status =='正常'"
            v-hasPermi="['residence:residence:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-close"
            @click="handleDelete(scope.row)"
            v-if="scope.row.status =='正常'"
            v-hasPermi="['residence:residence:remove']"
          >注销
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-user"
            @click="handleToPeople(scope.row)"
            v-if="scope.row.status =='正常'"
            v-hasPermi="['residence:residence:remove']"
          >人员信息
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-tickets"
            @click="handleShowHistory(scope.row)"
            v-hasPermi="['residence:residence:remove']"
          >历史操作记录
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改户籍管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="户籍性质" prop="type">
          <el-select v-model="form.type" placeholder="请选择户籍性质">
            <el-option
              v-for="dict in dict.type.household_registration_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="户号"  prop="householdsNo">
          <el-input v-model="form.householdsNo"></el-input>
        </el-form-item>
        <el-form-item label="是否为军属"  prop="isMilitaryFamilies">
          <el-select v-model="form.isMilitaryFamilies" placeholder="请选择是否为军属">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="户主身份证号"  prop="idCard">
          <el-input v-model="form.idCard"></el-input>
        </el-form-item>
        <el-form-item label="户主姓名"  prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="区县名称">
          <el-cascader
            style="width: 100%"
            :options="options"
            v-model="selectedOptions"
            @change="handleChangeAddress">
          </el-cascader>
        </el-form-item>
        <el-form-item label="户籍所在地"  prop="address">
          <el-input v-model="form.address"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改户籍管理对话框 -->
    <el-dialog title="历史操作记录" :visible.sync="historyOpen" width="700px" append-to-body>
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in activities"
          :key="index"
          :icon="'el-icon-more'"
          :type="activity.msg.indexOf('迁出')!=-1||activity.msg.indexOf('注销')!=-1?'danger':'primary'"
          :size="'large'"
          :timestamp="activity.createBy + '   '+activity.createTime">
          {{activity.msg}}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import { listResidence, getResidence, delResidence, addResidence, updateResidence } from '@/api/residence/residence'
import {regionData, CodeToText, TextToCode} from 'element-china-area-data'
import { listHistory } from '@/api/residence/history'
export default {
  name: 'Residence',
  dicts: ['household_registration_type','sys_yes_no','residence_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 户籍管理表格数据
      residenceList: [],
      statusOption:['正常','注销'],
      // 省市区联动
      options: regionData,
      historyOpen: false,
      // 默认显示
      selectedOptions: ['310000', '310100', '310115'],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        householdsNo: null,
        isMilitaryFamilies: null,
        idCard: null,
        name: null,
        province: null,
        city: null,
        county: null,
        address: null,
        type: null,
        visaAgencies: null
      },
      // 表单参数
      form: {},
      activities: [],
      // 表单校验
      rules: {
        type: [
          { required: true, message: '户籍性质不能为空', trigger: 'change' }
        ],
        householdsNo: [
          { required: true, message: '户号不能为空', trigger: 'blur' }
        ],
        isMilitaryFamilies: [
          { required: true, message: '请选择是否为军属', trigger: 'change' }
        ],
        idCard: [
          { required: true, message: '户主身份证号不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '户主姓名不能为空', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '户籍所在地不能为空', trigger: 'blur' }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询户籍管理列表 */
    getList() {
      this.loading = true
      listResidence(this.queryParams).then(response => {
        this.residenceList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        householdsNo: null,
        isMilitaryFamilies: null,
        idCard: null,
        name: null,
        province: null,
        city: null,
        county: null,
        address: null,
        type: null,
        visaAgencies: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    handleChangeAddress(value) {
      this.form.province = CodeToText[value[0]];
      this.form.city = CodeToText[value[1]];
      this.form.county = CodeToText[value[2]]
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.form = {
          type: '1',
          province:'上海市',
          city:'市辖区',
          county:'浦东新区',
          address: '',
        isMilitaryFamilies:'N'
      }
      this.open = true
      this.title = '添加户籍'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getResidence(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改户籍'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateResidence(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addResidence(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 注销按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认注销户籍？').then(function() {
        return delResidence(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('注销成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('residence/residence/export', {
        ...this.queryParams
      }, `residence_${new Date().getTime()}.xlsx`)
    },
    handleToPeople(row){
      this.$router.push({
        path:'/people',
        query: {
          householdRegisterId: row.id
        }
      })
    },
    handleShowHistory(row){
      this.historyOpen=true
      listHistory({householdRegisterId: row.id}).then(response=>{
        console.log(response)
        this.activities = response.data
      })
    }
  }
}
</script>
